package cn.itcast.bos.web.action.system;

import java.util.List;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;

import cn.itcast.bos.domain.system.Menu;
import cn.itcast.bos.domain.system.User;
import cn.itcast.bos.service.system.MenuService;
import cn.itcast.bos.web.action.common.BaseAction;
import oracle.net.aso.l;

@SuppressWarnings("serial")
@Namespace("/")
@ParentPackage("json-default")
@Controller
@Scope("prototype")
public class MenuAction extends BaseAction<Menu>{
	@Autowired
	private MenuService menuService;
	
	
	@Action(value="menu_list",results={@Result(name="success",type="json")})
	public String list(){
		//查询所有的菜单
		List<Menu> list = menuService.findAll();
		//压入值栈
	    ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
		
		
	}
	
	
	@Action(value="menu_save",results={@Result(name="success",type="redirect",location="pages/system/menu.html")})
	public String save(){
		menuService.save(model);
		return SUCCESS;
	}
	
	//动态菜单： 不同用户登录后，应该看到不同菜单结构
	@Action(value="menu_showmenu",results={@Result(name="success",type="json")})
	public String showMenu(){
		//从shiro框架中拿到登录的用户user信息
		Subject subject = SecurityUtils.getSubject();
	    User user  = (User) subject.getPrincipal();
	    //根据用户id查询相关联的菜单
	    List<Menu> list = menuService.findByUser(user);
	    ActionContext.getContext().getValueStack().push(list);
		return SUCCESS;
	}

}
